//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.farm.authority;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpSession;

import com.farm.authority.domain.Action;
import com.farm.authority.domain.User;
import com.farm.authority.service.ActionServiceInter;
import com.farm.authority.service.OutuserServiceInter;
import com.farm.authority.service.UserServiceInter;
import com.farm.core.AuthorityService;
import com.farm.core.auth.domain.AuthKey;
import com.farm.core.auth.domain.LoginUser;
import com.farm.core.auth.domain.WebMenu;
import com.farm.core.auth.exception.LoginUserNoExistException;
import com.farm.core.auth.util.AuthenticateInter;
import com.farm.core.auth.util.AuthenticateProvider;
import com.farm.util.spring.BeanFactory;
import com.farm.web.WebUtils;

public class FarmAuthorityService implements AuthorityService {
    private UserServiceInter userServiceImpl;
    private ActionServiceInter actionServiceImpl;
    private OutuserServiceInter outuserServiceImpl;
    private AuthenticateInter authUtil = AuthenticateProvider.getInstance();
    private static FarmAuthorityService service;

    public FarmAuthorityService() {
    }

    public static AuthorityService getInstance() {
        if (service == null) {
            service = new FarmAuthorityService();
            service.userServiceImpl = (UserServiceInter)BeanFactory.getBean("userServiceImpl");
            service.outuserServiceImpl = (OutuserServiceInter)BeanFactory.getBean("outuserServiceImpl");
            service.actionServiceImpl = (ActionServiceInter)BeanFactory.getBean("actionServiceImpl");
        }

        return service;
    }

    public static void loginIntoSession(HttpSession session, String ip, String loginName) {
    	WebUtils webutils = new WebUtils();
        webutils.setCurrentUser(getInstance().getUserByLoginName(loginName), session);
        webutils.setLoginTime(session);
        webutils.setCurrentUserAction(getInstance().getUserAuthKeys(webutils.getCurrentUser(session).getId()), session);
        webutils.setCurrentUserMenu(getInstance().getUserMenu(webutils.getCurrentUser(session).getId()), session);
        getInstance().loginHandle(webutils.getCurrentUser(session).getId());
    }

    public void loginHandle(String userId) {
        this.userServiceImpl.setLoginTime(userId);
    }

    public Set<String> getUserAuthKeys(String userId) {
        User user = this.userServiceImpl.getUserEntity(userId);
        List<Action> actions = null;
        if (user.getType().equals("3")) {
            actions = this.actionServiceImpl.getAllActions();
        } else {
            actions = this.userServiceImpl.getUserActions(userId);
        }

        Set<String> set = new HashSet();
        Iterator var6 = actions.iterator();

        while(var6.hasNext()) {
            Action action = (Action)var6.next();
            set.add(action.getAuthkey());
        }

        return set;
    }

    public LoginUser getUserById(String userId) {
        return this.userServiceImpl.getUserEntity(userId);
    }

    public LoginUser getUserByLoginName(String loginName) {
        return this.userServiceImpl.getUserByLoginName(loginName);
    }

    public List<WebMenu> getUserMenu(String userId) {
        User user = this.userServiceImpl.getUserEntity(userId);
        List<WebMenu> list = null;
        if (user.getType().equals("3")) {
            list = this.actionServiceImpl.getAllMenus();
        } else {
            list = this.userServiceImpl.getUserMenus(userId);
        }

        return list;
    }

    public boolean isLegality(String loginName, String password) throws LoginUserNoExistException {
        User user = this.userServiceImpl.getUserByLoginName(loginName);
        if (user == null) {
            throw new LoginUserNoExistException("该登录名不存在！");
        } else if (user.getType().equals("2")) {
            throw new LoginUserNoExistException("该用户无登录权限！");
        } else if (!user.getState().equals("1")) {
            throw new LoginUserNoExistException("该用户已停用！");
        } else {
            if (this.authUtil.isMd5code(password)) {
                if (password.toUpperCase().equals(user.getPassword())) {
                    return true;
                }
            } else if (this.authUtil.encodeLoginPasswordOnMd5(password, loginName).equals(user.getPassword())) {
                return true;
            }

            return false;
        }
    }

    public AuthKey getAuthKey(String key) {
        return this.actionServiceImpl.getCacheAction(key);
    }

    public List<String> getUserPostKeys(String userId) {
        return this.userServiceImpl.getUserPostIds(userId);
    }

    public String getUserOrgKey(String userId) {
        return this.userServiceImpl.getUserOrganization(userId).getId();
    }

    public UserServiceInter getUserServiceImpl() {
        return this.userServiceImpl;
    }

    public void setUserServiceImpl(UserServiceInter userServiceImpl) {
        this.userServiceImpl = userServiceImpl;
    }

    public ActionServiceInter getActionServiceImpl() {
        return this.actionServiceImpl;
    }

    public void setActionServiceImpl(ActionServiceInter actionServiceImpl) {
        this.actionServiceImpl = actionServiceImpl;
    }

    public LoginUser getUserByOutUserId(String outuserid, String name, String content) {
        return this.outuserServiceImpl.getUserByOutUserId(outuserid, name, content);
    }
}
